Completed
Push — master ( 77d906...cc193b )
by Jean
01:28
created

configure.test.js ➔ ... ➔ describe   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 46
rs 8.9411
1
import chai from 'chai';
2
import chaiaspromised from 'chai-as-promised';
3
import mockfs from 'mock-fs';
4
5
import configure from '../src/configure.js';
6
7
let expect = chai.expect;
8
chai.use(chaiaspromised);
9
10
describe(`[module] configure`, function() {
11
	describe(`[method] load`, function() {
12
		let defaultconfpath = `.codingamerc`;
13
		let incorrectconfpath = `.codingamerc.txt`;
14
		let defaultconf = {
15
			"username": `me`,
16
			"password": `somepass`,
17
			"exercise": `5711567e959cf54dd2dd79c1b4c259560d6ba46`,
18
			"tests": [1, 2],
19
			"language": `Python`,
20
			"bundle": `bundle.py`
21
		};
22
		before(function() {
23
			mockfs({
24
				[defaultconfpath]: JSON.stringify(defaultconf),
25
				[incorrectconfpath]: `This is not valid JSON!`
26
			});
27
		});
28
		after(function() {
29
			mockfs.restore();
30
		});
31
		afterEach(function() {
32
			for (let param in defaultconf) {
33
				configure.forget(param);
34
			}
35
		});
36
37
		it(`should return the content of file parsed as JSON`, function() {
38
			let conf = configure.load(defaultconfpath);
39
			return expect(conf).to.eventually.be.deep.equal(defaultconf);
40
		});
41
42
		it(`should reject because of incorrect file path`, function() {
43
			let conf = configure.load(`/incorrect/path/.codingamerc`);
44
			return expect(conf).to.eventually.be.rejected;
45
		});
46
47
		it(`should reject because of incorrect parsing of JSON`, function() {
48
			let conf = configure.load(incorrectconfpath);
49
			return expect(conf).to.eventually.be.rejected;
50
		})
51
52
		it(`should return options if path is incorrect and options is defined`, function() {
53
			let conf = configure.load(undefined, defaultconf);
54
			return expect(conf).to.eventually.be.deep.equal(defaultconf);
55
		});
56
	});
57
	describe(`[method] get`, function() {
58
		let filepath = `file.txt`;
59
		let filecontent = `Hello world!`
60
		let shellcmd = [`echo`, filecontent];
61
		let defaultconf = {
62
			"shell": shellcmd,
63
			"wrongshell": [`notacommand`],
64
			"path": filepath
65
		};
66
		before(function(done) {
67
			mockfs({
68
				[filepath]: filecontent
69
			});
70
			configure.load(undefined, defaultconf).then(function() {
71
				done();
72
			})
73
		});
74
		after(function() {
75
			mockfs.restore();
76
			for (let param in defaultconf) {
77
				configure.forget(param);
78
			}
79
		});
80
		it(`should return the string property as it is`, function() {
81
			let get = configure.get(`path`);
82
			return expect(get).to.eventually.be.equal(filepath);
83
		});
84
		it(`should return the array property as it is`, function() {
85
			let get = configure.get(`shell`);
86
			return expect(get).to.eventually.be.deep.equal(shellcmd);
87
		});
88
		it(`should return the result of the shell command`, function() {
89
			let get = configure.get(`shell`, `shell`);
90
			return expect(get).to.eventually.be.deep.equal(filecontent);
91
		});
92
		it(`should return the content of the file`, function() {
93
			let get = configure.get(`path`, `file`);
94
			return expect(get).to.eventually.be.deep.equal({
95
				"path": filepath,
96
				"data": filecontent
97
			});
98
		});
99
		it(`should reject because property doesn't exist`, function() {
100
			let get = configure.get(`notaproperty`);
101
			return expect(get).to.eventually.be.rejected;
102
		});
103
		it(`should reject if first parameter is not a string`, function() {
104
			let get = configure.get(null);
105
			return expect(get).to.eventually.be.rejected;
106
		});
107
		it(`should reject if shell command is failing`, function() {
108
			let get = configure.get(`wrongshell`, `shell`);
109
			return expect(get).to.eventually.be.rejected;
110
		});
111
	});
112
	describe(`[method] forget`, function() {
113
		let conf = {
114
			"property": `value`
115
		};
116
		before(function() {
117
			configure.load(undefined, conf);
118
		})
119
		it(`should forget about the parameter`, function() {
120
			let forget = configure.forget(`property`);
0 ignored issues
show
Unused Code introduced by
The assignment to variable forget seems to be never used. Consider removing it.
Loading history...
121
			let get = configure.get(`property`);
122
			return expect(get).to.eventually.be.rejected;
123
		});
124
		it(`should throw an error when the property is not a string`, function() {
125
			try {
126
				configure.forget(null);
127
			} catch(error) {
128
				return expect(error).to.be.an('error');
129
			}
130
			return expect(false).to.be.ok;
131
		});
132
	});
133
});
134